package com.xxxx.paipaicars.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * @author 北海不是海
 * @version 1.0
 * Create by 2023-06-11 21:41
 * 使用Spring上下文的方方式进行对Bean的注入
 */

public class SpringUtil implements ApplicationContextAware {

    //为Spring的高级容器
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    //获取Spring的上下文
    public static ApplicationContext getApplicationContext(){
        return SpringUtil.applicationContext;
    }

    public static <T> T getBean(String name){
        return (T)getApplicationContext().getBean(name);
    }

    public static <T> T  getBean(String name , Class<T> clazz){
        return (T)getApplicationContext().getBean(name,clazz);
    }
}
